#ifndef ROPLAYER_VALIDATOR_H
#define ROPLAYER_VALIDATOR_H

//Player的拦截器，用于在客户端发起登录请求时做过滤处理

#include <QObject>
#include <QtQmlIntegration>

class ROPlayerEvent:public QObject
{
    Q_OBJECT
public:
    ROPlayerEvent(QObject* parent = nullptr);
    bool accepted() const;
    void setAccepted(bool newAccepted);

    QVariantMap loginParam() const;
    void setLoginParam(const QVariantMap &newLoginParam);




    QString rejectMessage() const;
    void setRejectMessage(const QString &newRejectMessage);

signals:
    void acceptedChanged();
    void loginParamChanged();
    void rejectMessageChanged();

private:
    bool m_accepted = false;//是否允许玩家登录
    QVariantMap m_loginParam;//该玩家的登录参数
    QString m_rejectMessage;//拒绝玩家时的信息
    Q_PROPERTY(bool accepted READ accepted WRITE setAccepted NOTIFY acceptedChanged FINAL)
    Q_PROPERTY(QVariantMap loginParam READ loginParam WRITE setLoginParam NOTIFY loginParamChanged FINAL)
    Q_PROPERTY(QString rejectMessage READ rejectMessage WRITE setRejectMessage NOTIFY rejectMessageChanged FINAL)
};

class ROPlayerValidator:public QObject
{
    Q_OBJECT
    QML_ELEMENT

public:
    enum Policy
    {
        None,
        HostOnly = 0x01,//验证仅对房主有用
        ClientOnly = 0x02,//验证仅对除了房主以外的玩家有用
        All = HostOnly | ClientOnly//验证对所有玩家都有用
    };
    Q_ENUM(Policy);

public:
    ROPlayerValidator(QObject* parent = nullptr);

    ROPlayerEvent* validate(const QVariantMap& loginParam);

    Policy policy() const;
    void setPolicy(const Policy &newPolicy);

signals:
    void playerJoin(ROPlayerEvent* event);//玩家加入事件

    void policyChanged();

private:
    ROPlayerEvent* m_event = nullptr;
    Policy m_policy = All;
    Q_PROPERTY(Policy policy READ policy WRITE setPolicy NOTIFY policyChanged FINAL)
};

#endif
